package com.smcdd.cdb.dbo.cdb_dbo.ui.activity;

import android.os.Handler;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.smcdd.cdb.dbo.cdb_dbo.R;
import com.smcdd.cdb.dbo.cdb_dbo.calendar.DateUtils;
import com.smcdd.cdb.dbo.cdb_dbo.calendar.MonthDateView;
import com.smcdd.cdb.dbo.cdb_dbo.service.presenter.HistoryReachPresenter;
import com.smcdd.cdb.dbo.cdb_dbo.service.view.IHistoryReach;
import com.smcdd.cdb.dbo.cdb_dbo.ui.base.BaseActivity;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.CommonUtils;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.GetWeek;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 作者：Administrator on 2017/7/10 14:49
 * 邮箱：zhanghuaiha@gmail.com
 * 历史达标
 */

public class HistoryReachActivity extends BaseActivity<IHistoryReach,HistoryReachPresenter> implements IHistoryReach{

    private ImageView iv_left;
    private ImageView iv_right;
    private TextView tv_date;
    private TextView tv_week;
    private TextView tv_today;
    private TextView add_title;
    private MonthDateView monthDateView;
    private String start;
    private ImageView back;
    private String end;


    @Override
    public void initView() {
        List<Integer> list = new ArrayList<Integer>();
        monthDateView = (MonthDateView) findViewById(R.id.monthDateView);
        tv_date = (TextView) findViewById(R.id.date_text);
        tv_week  =(TextView) findViewById(R.id.week_text);
        iv_left = (ImageView) findViewById(R.id.iv_left);
        iv_right = (ImageView) findViewById(R.id.iv_right);
        back = (ImageView) findViewById(R.id.history_back);
        tv_today = (TextView) findViewById(R.id.tv_today);
        add_title = (TextView) findViewById(R.id.add_title);
        monthDateView.setTextView(tv_date,tv_week);
        monthDateView.setDaysHasThingList(list);
    }

    @Override
    public void initData() {
        String sid = getIntent().getStringExtra("sid");
        String created = getIntent().getStringExtra("created");
        String name = getIntent().getStringExtra("name");
        add_title.setText(name);
        Log.e("time",created);
        String time = com.smcdd.cdb.dbo.cdb_dbo.ui.utils.DateUtils.getTime(System.currentTimeMillis(), "yyyy-MM-dd");

        monthDateView.notDay(created,time);
        mPresenter.loadDate(sid);
        end = getTime(new Date(System.currentTimeMillis()));
        start = end.substring(0, end.length() - 2) + "01";
       // mPresenter.getDate(start, end);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                refresh();
            }
        },1000);

    }

    @Override
    public void initListener() {

        back.setOnClickListener(v -> finish());
        iv_left.setOnClickListener(v -> {
            if (!CommonUtils.isFastDoubleClick()) {
                long time_long = getTime_long(monthDateView.getmSelYear() + "-" + monthDateView.getmSelMonth() + "-01");
                long time = getTime_long("2017-06-02");
                if (time_long > time) {
                    onLeftClick();
                }else {
                    return;
                }
            }
        });

        iv_right.setOnClickListener(v -> {
            if (!CommonUtils.isFastDoubleClick()) {
                long time_long = getTime_long(monthDateView.getmSelYear() + "-" + monthDateView.getmSelMonth() + "-02");
                long time = getTime_long(start);
                if (time_long <= time) {
                    onRightClick();
                }else {
                    return;
                }
            }
        });

        tv_today.setOnClickListener(v -> {
            if (!CommonUtils.isFastDoubleClick()) {
                mPresenter.getDate(start, end,3);
            }
           // monthDateView.setTodayToView();
        });
    }

    public void refresh(){
        mPresenter.getDate(start, end,3);
        monthDateView.setTodayToView();
    }


    private String getTime(Date date) {//可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    private long getTime_long(String time){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return  format.parse(time).getTime();

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 左点击，日历向后翻页
     */
    public void onLeftClick(){
        int year = monthDateView.getmSelYear();
        int month = monthDateView.getmSelMonth();
        if(month == 0){//若果是1月份，则变成12月份
            year = monthDateView.getmSelYear()-1;
            month = 11;
        }else{
            month = month-1;
        }
        int daysByYearMonth = GetWeek.getDaysByYearMonth(year, month);
        if (month > 9) {
            mPresenter.getDate(year+"-"+month+"-"+"01",year+"-"+month+"-"+daysByYearMonth,1);
        }else {
            mPresenter.getDate(year+"-0"+month+"-"+"01",year+"-0"+month+"-"+daysByYearMonth,1);
        }
        System.out.println(year+"-0"+month+"-"+"01"+"-----"+year+"-0"+month+"-"+daysByYearMonth);

    }

    /**
     * 右点击，日历向前翻页
     */
    public void onRightClick(){
        int year = monthDateView.getmSelYear();
        int month = monthDateView.getmSelMonth();
        if(month == 11) {//若果是12月份，则变成1月份
            year = monthDateView.getmSelYear() + 1;
            month = 0;
        }else{
            month = month + 1;
        }
        int daysByYearMonth = GetWeek.getDaysByYearMonth(year, month);
        if (month > 9) {
            mPresenter.getDate(year+"-"+month+"-"+"01",year+"-"+month+"-"+daysByYearMonth,2);
        }else {
            mPresenter.getDate(year+"-0"+month+"-"+"01",year+"-0"+month+"-"+daysByYearMonth,2);
        }
        System.out.println(year+"-0"+month+"-"+"01"+"-----"+year+"-0"+month+"-"+daysByYearMonth);

    }


    @Override
    protected HistoryReachPresenter createPresenter() {
        return new HistoryReachPresenter(this);
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_history;
    }


    @Override
    public MonthDateView getMDV() {
        return monthDateView;
    }

    @Override
    public HistoryReachActivity getAC() {
        return HistoryReachActivity.this;
    }
}
